package business;

import java.util.Calendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class OrderList {

    private int jumlahOrder = -1;

    public OrderList() {
        emf = Persistence.createEntityManagerFactory("apoltekkPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /**
     * @return the jumlahOrder
     */
    public int getJumlahOrder() {

        if (jumlahOrder == -1) {
            EntityManager em = null;
            try {
                em = getEntityManager();
                Query q = em.createQuery("SELECT count(o) order FROM order as o");
                Number jumlah = (Number) q.getSingleResult();
                jumlahOrder = jumlah.intValue();

            } catch (javax.persistence.EntityNotFoundException e) {
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }

        return jumlahOrder;
    }

    public int getJumlahOrderAd(String Admin) {

        if (jumlahOrder == -1) {
            EntityManager em = null;
            try {
                em = getEntityManager();
                Query q = em.createQuery("SELECT count(o) order FROM order o, obat l WHERE o.obat= l.kode and l.admin_username = :username");
                q.setParameter("username", Admin);
                Number jumlah = (Number) q.getSingleResult();
                jumlahOrder = jumlah.intValue();

            } catch (javax.persistence.EntityNotFoundException e) {
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }

        return jumlahOrder;
    }

    public List<OrderList> OrderTerbaru() {
        List<OrderList> Order = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM order o, obat l, user m WHERE o.kode = l.kode AND o.username = m.username ORDER BY o.kode");
            Order = q.getResultList();

        } catch (javax.persistence.EntityNotFoundException e) {
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return Order;
    }

    public Order getOrder (Long idOrder){
        Order l  = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM order as o WHERE o.idOrder= :idOrder");
            q.setParameter("idOrder", idOrder);
            l = (Order) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return l;
    }

    public Order getBO (String User){
        Order ib  = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM order o WHERE o.user= :username");
            q.setParameter("username", User);
            ib = (Order) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return ib;
    }

    public Order getBOp (String Admin){
        Order ib  = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM order o, obat l WHERE o.obat= l.kode and l.admin_username = :username");
            q.setParameter("username", Admin);
            ib = (Order) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return ib;
    }


    public void tambahOrder(Order Order) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Order.setorderdate(Calendar.getInstance().getTime());
            em.persist(Order);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void hapusOrder(Order Order) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(Order);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void editOrder(Order Order) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(Order);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }
}
